package com.hilin.hilinim;

import com.hilin.common.common.handler.RequestTokenHandler;
import com.hilin.common.util.EncryptUtils;
import com.hilin.common.util.JwtUtils;
import com.hilin.entity.po.AccountInfoPo;
import com.hilin.entity.vo.SignVo;
import com.hilin.hilinim.netty.ws.WsConnector;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableEurekaClient
@MapperScans({
        @MapperScan("com.hilin.hilinim.mapper.mysql")
})
@EnableSwagger2
@Slf4j
@SpringBootApplication
public class HilinImApplication extends WebMvcConfigurationSupport implements ApplicationRunner {

    static {
        String token = JwtUtils.generateJWT(
                AccountInfoPo.builder()
                        .account(1L)
                        .accountName("name1")
                        .orgId(1L)
                        .build(), 0);
        log.info("生成token(appid:accountId):{}", token);
        SignVo signVo = SignVo.builder()
                .orgId(123L)
                .nonce("123456")
                .timestamp(System.currentTimeMillis())
                .build();
        log.info("管理员 -> appid={}&nonce={}&timestamp={}&sign={}", signVo.getOrgId(), signVo.getNonce(), signVo.getTimestamp(),
                EncryptUtils.getMd5(signVo.getOrgId() + signVo.getNonce() + signVo.getTimestamp() + "123"));

        log.info("用户:123 -> account={}&appid={}&nonce={}&timestamp={}&sign={}", "123", signVo.getOrgId(), signVo.getNonce(), signVo.getTimestamp(),
                EncryptUtils.getMd5("123" + signVo.getOrgId() + signVo.getNonce() + signVo.getTimestamp() + "123"));
    }

    @Autowired
    private WsConnector wsConnector;

    public static void main(String[] args) {
        String str = "111.png";
        System.out.println(str.substring(str.lastIndexOf(".")));
//        SpringApplication.run(HilinImApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        wsConnector.start();
    }

    @Autowired
    private RequestTokenHandler requestTokenHandler;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(requestTokenHandler);
    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // .groupName("所有接口")
                // apiInfo指定测试文档基本信息，这部分将在页面展示
                .apiInfo(apiInfo()).select()
                // apis() 控制哪些接口暴露给swagger，
                // RequestHandlerSelectors.any() 所有都暴露
                // 指定包位置
                .apis(RequestHandlerSelectors.basePackage("com.hilin.hilinim.controller")).paths(PathSelectors.any())
                .build();
    }

    // 基本信息，页面展示
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("im")
                // 版本号
                .version("1.0.0-SNAPSHOT").build();
    }
}
